package com.duyj.rule.rhino;

import com.duyj.rule.model.datarow.DataRow;
import com.duyj.rule.model.dto.UserDTO;
import org.springframework.stereotype.Service;

@Service
public class DataRowRuleService {

    public String getValueJs(String tableName, String columnName) {
        return String.format("return getColumnDataValue('%s','%s');", tableName, columnName);
    }

    public String buildServerJs(String executeJs) {
        // 构建完整的脚本执行，包括脚本应用场景的上下文支撑脚本、对象支撑脚本。
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("function _main(){%n\t%s%n}%n", executeJs));
        sb.append("_main();");
        return sb.toString();
    }

    public UserDTO getUser_52GetSet(DataRow dataRow) {
        ScriptEngine s = new ScriptEngine();
        s.injectionObjectList(new DataRowAccessScriptableObject(dataRow));
        UserDTO userDTO = new UserDTO();
        Object userCode = s.execute(buildServerJs(getValueJs("tableName", "userCode")));
        userDTO.setUserCode(userCode.toString());
        Object userName = s.execute(buildServerJs(getValueJs("tableName", "userName")));
        userDTO.setUserName(userName.toString());
        Object f1 = s.execute(buildServerJs(getValueJs("tableName", "f1")));
        Object f2 = s.execute(buildServerJs(getValueJs("tableName", "f2")));
        Object f3 = s.execute(buildServerJs(getValueJs("tableName", "f3")));
        Object f4 = s.execute(buildServerJs(getValueJs("tableName", "f4")));
        Object f5 = s.execute(buildServerJs(getValueJs("tableName", "f5")));
        Object f6 = s.execute(buildServerJs(getValueJs("tableName", "f6")));
        Object f7 = s.execute(buildServerJs(getValueJs("tableName", "f7")));
        Object f8 = s.execute(buildServerJs(getValueJs("tableName", "f8")));
        Object f9 = s.execute(buildServerJs(getValueJs("tableName", "f9")));
        Object f10 = s.execute(buildServerJs(getValueJs("tableName", "f10")));
        Object f11 = s.execute(buildServerJs(getValueJs("tableName", "f11")));
        Object f12 = s.execute(buildServerJs(getValueJs("tableName", "f12")));
        Object f13 = s.execute(buildServerJs(getValueJs("tableName", "f13")));
        Object f14 = s.execute(buildServerJs(getValueJs("tableName", "f14")));
        Object f15 = s.execute(buildServerJs(getValueJs("tableName", "f15")));
        Object f16 = s.execute(buildServerJs(getValueJs("tableName", "f16")));
        Object f17 = s.execute(buildServerJs(getValueJs("tableName", "f17")));
        Object f18 = s.execute(buildServerJs(getValueJs("tableName", "f18")));
        Object f19 = s.execute(buildServerJs(getValueJs("tableName", "f19")));
        Object f20 = s.execute(buildServerJs(getValueJs("tableName", "f20")));
        Object f21 = s.execute(buildServerJs(getValueJs("tableName", "f21")));
        Object f22 = s.execute(buildServerJs(getValueJs("tableName", "f22")));
        Object f23 = s.execute(buildServerJs(getValueJs("tableName", "f23")));
        Object f24 = s.execute(buildServerJs(getValueJs("tableName", "f24")));
        Object f25 = s.execute(buildServerJs(getValueJs("tableName", "f25")));
        Object f26 = s.execute(buildServerJs(getValueJs("tableName", "f26")));
        Object f27 = s.execute(buildServerJs(getValueJs("tableName", "f27")));
        Object f28 = s.execute(buildServerJs(getValueJs("tableName", "f28")));
        Object f29 = s.execute(buildServerJs(getValueJs("tableName", "f29")));
        Object f30 = s.execute(buildServerJs(getValueJs("tableName", "f30")));
        Object f31 = s.execute(buildServerJs(getValueJs("tableName", "f31")));
        Object f32 = s.execute(buildServerJs(getValueJs("tableName", "f32")));
        Object f33 = s.execute(buildServerJs(getValueJs("tableName", "f33")));
        Object f34 = s.execute(buildServerJs(getValueJs("tableName", "f34")));
        Object f35 = s.execute(buildServerJs(getValueJs("tableName", "f35")));
        Object f36 = s.execute(buildServerJs(getValueJs("tableName", "f36")));
        Object f37 = s.execute(buildServerJs(getValueJs("tableName", "f37")));
        Object f38 = s.execute(buildServerJs(getValueJs("tableName", "f38")));
        Object f39 = s.execute(buildServerJs(getValueJs("tableName", "f39")));
        Object f41 = s.execute(buildServerJs(getValueJs("tableName", "f41")));
        Object f42 = s.execute(buildServerJs(getValueJs("tableName", "f42")));
        Object f43 = s.execute(buildServerJs(getValueJs("tableName", "f43")));
        Object f44 = s.execute(buildServerJs(getValueJs("tableName", "f44")));
        Object f45 = s.execute(buildServerJs(getValueJs("tableName", "f45")));
        Object f46 = s.execute(buildServerJs(getValueJs("tableName", "f46")));
        Object f47 = s.execute(buildServerJs(getValueJs("tableName", "f47")));
        Object f48 = s.execute(buildServerJs(getValueJs("tableName", "f48")));
        Object f49 = s.execute(buildServerJs(getValueJs("tableName", "f49")));


        userDTO.setF1(f1.toString());
        userDTO.setF2(f2.toString());
        userDTO.setF3(f3.toString());
        userDTO.setF4(f4.toString());
        userDTO.setF5(f5.toString());
        userDTO.setF6(f6.toString());
        userDTO.setF7(f7.toString());
        userDTO.setF8(f8.toString());
        userDTO.setF9(f9.toString());
        userDTO.setF10(f10.toString());
        userDTO.setF11(f11.toString());
        userDTO.setF12(f12.toString());
        userDTO.setF13(f13.toString());
        userDTO.setF14(f14.toString());
        userDTO.setF15(f15.toString());
        userDTO.setF16(f16.toString());
        userDTO.setF17(f17.toString());
        userDTO.setF18(f18.toString());
        userDTO.setF19(f19.toString());
        userDTO.setF20(f20.toString());
        userDTO.setF21(f21.toString());
        userDTO.setF22(f22.toString());
        userDTO.setF23(f23.toString());
        userDTO.setF24(f24.toString());
        userDTO.setF25(f25.toString());
        userDTO.setF26(f26.toString());
        userDTO.setF27(f27.toString());
        userDTO.setF28(f28.toString());
        userDTO.setF29(f29.toString());
        userDTO.setF30(f30.toString());
        userDTO.setF31(f31.toString());
        userDTO.setF32(f32.toString());
        userDTO.setF33(f33.toString());
        userDTO.setF34(f34.toString());
        userDTO.setF35(f35.toString());
        userDTO.setF36(f36.toString());
        userDTO.setF37(f37.toString());
        userDTO.setF38(f38.toString());
        userDTO.setF39(f39.toString());
        userDTO.setF41(f41.toString());
        userDTO.setF42(f42.toString());
        userDTO.setF43(f43.toString());
        userDTO.setF44(f44.toString());
        userDTO.setF45(f45.toString());
        userDTO.setF46(f46.toString());
        userDTO.setF47(f47.toString());
        userDTO.setF48(f48.toString());
        userDTO.setF49(f49.toString());
        return userDTO;
    }

    public UserDTO getUser_2GetSet(DataRow dataRow) {
        ScriptEngine s = new ScriptEngine();
        s.injectionObjectList(new DataRowAccessScriptableObject(dataRow));
        UserDTO userDTO = new UserDTO();
        Object userCode = s.execute(buildServerJs(getValueJs("tableName", "userCode")));
        userDTO.setUserCode(userCode.toString());
        Object userName = s.execute(buildServerJs(getValueJs("tableName", "userName")));
        userDTO.setUserName(userName.toString());
        return userDTO;
    }

    private boolean init = false;

    ScriptEngine scriptEngine;

    private void init(DataRow dataRow) {
        if (init) {
            return;
        }
        scriptEngine = new ScriptEngine();
        scriptEngine.injectionObjectList(new DataRowAccessScriptableObject(dataRow));
        init = true;
    }


    public UserDTO getUserRuleWithCache_52GetSet(DataRow dataRow) {
        init(dataRow);
        UserDTO userDTO = new UserDTO();
        Object userCode = scriptEngine.execute(buildServerJs(getValueJs("tableName", "userCode")));
        userDTO.setUserCode(userCode.toString());
        Object userName = scriptEngine.execute(buildServerJs(getValueJs("tableName", "userName")));
        userDTO.setUserName(userName.toString());
        Object f1 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f1")));
        Object f2 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f2")));
        Object f3 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f3")));
        Object f4 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f4")));
        Object f5 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f5")));
        Object f6 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f6")));
        Object f7 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f7")));
        Object f8 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f8")));
        Object f9 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f9")));
        Object f10 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f10")));
        Object f11 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f11")));
        Object f12 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f12")));
        Object f13 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f13")));
        Object f14 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f14")));
        Object f15 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f15")));
        Object f16 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f16")));
        Object f17 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f17")));
        Object f18 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f18")));
        Object f19 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f19")));
        Object f20 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f20")));
        Object f21 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f21")));
        Object f22 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f22")));
        Object f23 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f23")));
        Object f24 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f24")));
        Object f25 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f25")));
        Object f26 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f26")));
        Object f27 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f27")));
        Object f28 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f28")));
        Object f29 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f29")));
        Object f30 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f30")));
        Object f31 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f31")));
        Object f32 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f32")));
        Object f33 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f33")));
        Object f34 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f34")));
        Object f35 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f35")));
        Object f36 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f36")));
        Object f37 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f37")));
        Object f38 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f38")));
        Object f39 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f39")));
        Object f41 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f41")));
        Object f42 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f42")));
        Object f43 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f43")));
        Object f44 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f44")));
        Object f45 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f45")));
        Object f46 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f46")));
        Object f47 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f47")));
        Object f48 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f48")));
        Object f49 = scriptEngine.execute(buildServerJs(getValueJs("tableName", "f49")));


        userDTO.setF1(f1.toString());
        userDTO.setF2(f2.toString());
        userDTO.setF3(f3.toString());
        userDTO.setF4(f4.toString());
        userDTO.setF5(f5.toString());
        userDTO.setF6(f6.toString());
        userDTO.setF7(f7.toString());
        userDTO.setF8(f8.toString());
        userDTO.setF9(f9.toString());
        userDTO.setF10(f10.toString());
        userDTO.setF11(f11.toString());
        userDTO.setF12(f12.toString());
        userDTO.setF13(f13.toString());
        userDTO.setF14(f14.toString());
        userDTO.setF15(f15.toString());
        userDTO.setF16(f16.toString());
        userDTO.setF17(f17.toString());
        userDTO.setF18(f18.toString());
        userDTO.setF19(f19.toString());
        userDTO.setF20(f20.toString());
        userDTO.setF21(f21.toString());
        userDTO.setF22(f22.toString());
        userDTO.setF23(f23.toString());
        userDTO.setF24(f24.toString());
        userDTO.setF25(f25.toString());
        userDTO.setF26(f26.toString());
        userDTO.setF27(f27.toString());
        userDTO.setF28(f28.toString());
        userDTO.setF29(f29.toString());
        userDTO.setF30(f30.toString());
        userDTO.setF31(f31.toString());
        userDTO.setF32(f32.toString());
        userDTO.setF33(f33.toString());
        userDTO.setF34(f34.toString());
        userDTO.setF35(f35.toString());
        userDTO.setF36(f36.toString());
        userDTO.setF37(f37.toString());
        userDTO.setF38(f38.toString());
        userDTO.setF39(f39.toString());
        userDTO.setF41(f41.toString());
        userDTO.setF42(f42.toString());
        userDTO.setF43(f43.toString());
        userDTO.setF44(f44.toString());
        userDTO.setF45(f45.toString());
        userDTO.setF46(f46.toString());
        userDTO.setF47(f47.toString());
        userDTO.setF48(f48.toString());
        userDTO.setF49(f49.toString());
        return userDTO;
    }


    public UserDTO getUserRuleWithCache_2GetSet(DataRow dataRow) {
        init(dataRow);
        UserDTO userDTO = new UserDTO();
        Object userCode = scriptEngine.execute(buildServerJs(getValueJs("tableName", "userCode")));
        userDTO.setUserCode(userCode.toString());
        Object userName = scriptEngine.execute(buildServerJs(getValueJs("tableName", "userName")));
        userDTO.setUserName(userName.toString());
        return userDTO;
    }
}
